package cn.shengongying.admin.controller;

import cn.shengongying.admin.dto.BossOrderInDTO;
import cn.shengongying.bean.DataTablePageResult;
import cn.shengongying.bean.ResponseResult;
import cn.shengongying.enums.PayEnum;
import cn.shengongying.model.BossOrder;
import cn.shengongying.model.BossPosition;
import cn.shengongying.service.IBossOrderService;
import cn.shengongying.service.IBossPositionService;
import cn.shengongying.service.PayFactory;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * author: RXK
 * date: 2022/2/10 星期四
 * desc:
 */
@Controller
@RequestMapping("/sys/boss/order")
public class BossOrderController extends BaseController {


    private final PayFactory payFactory;

    private final IBossOrderService bossOrderService;

    private final IBossPositionService bossPositionService;

    public BossOrderController(PayFactory payFactory, IBossOrderService bossOrderService, IBossPositionService bossPositionService) {
        this.payFactory = payFactory;
        this.bossOrderService = bossOrderService;
        this.bossPositionService = bossPositionService;
    }

    @RequestMapping("/index")
    public String index() {
        return "/boss_order/index";
    }

    @ResponseBody
    @RequestMapping("/list")
    public DataTablePageResult<List<BossOrder>> list(@RequestBody BossOrderInDTO inDTO) {
        List<BossOrder> bossOrderList = bossOrderService.listAndPage(inDTO.getStart(), inDTO.getLength());
        Set<Long> positionIds = bossOrderList.stream()
                .map(BossOrder::getExtId)
                .collect(Collectors.toSet());
        if (CollectionUtils.isNotEmpty(positionIds)) {
            List<BossPosition> bossPositions = bossPositionService.listByIds(positionIds);
            bossOrderList.forEach(bossOrder -> {
                bossPositions.stream()
                        .filter(position -> position.getId()
                                .compareTo(bossOrder.getExtId()) == 0)
                        .findFirst()
                        .ifPresent(position -> {
                            bossOrder.setPositionName(position.getPositionName());
                            bossOrder.setMemberNumber(position.getMemberNumber());
                        });
            });
        }
        inDTO.setData(bossOrderList);
        long count = bossOrderService.countRefundOrder();
        inDTO.setRecordsTotal(count);
        return inDTO;
    }


    @ResponseBody
    @RequestMapping("/update/status")
    public ResponseResult<String> updateStatus(@RequestBody BossOrderInDTO inDTO) {

        BossOrder bossOrder = bossOrderService.getById(inDTO.getId());
        if (null == bossOrder || bossOrder.getStatus() != 3) {
            return ResponseResult.fail();
        }

        BossPosition position = bossPositionService.getById(bossOrder.getExtId());
        BigDecimal refundAmount = bossOrder.getServiceCharge()
                .multiply(BigDecimal.valueOf(bossOrder.getNumber() - position.getMemberNumber()));
        if (refundAmount.compareTo(BigDecimal.ZERO) <= 0) {
            return ResponseResult.fail();
        }

        bossOrder.setRefundAmount(refundAmount);
        payFactory.refund(PayEnum.wechat,bossOrder);
        return ResponseResult.success();
    }


}
